package com.gzhryc.net_marketing.cloud.micro.api.controllers.callback;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.pay.adaptation.weixin.WXPayUtils;
import com.gzhryc.pay.adaptation.weixin.msg.WXPayNotify;
import com.gzhryc.pay.adaptation.weixin.msg.WXPayNotifyResponse;
import com.gzhryc.pay.adaptation.weixin.msg.native_pay.WXPayNativePayNotify;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayConfig;
import com.gzhryc.shared_device.common.payment.services.WXPayConfigService;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountDepositOrderService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WXPayNativeCallbackController {

    static Logger log = Logger.getLogger(WXPayNativeCallbackController.class);

    @Route(value = "/paycall/wxpay/{appId}/native/pay")
    public WXPayNotifyResponse payResult(HttpServletRequest request, HttpServletResponse response) {
        String appId = WebUtils.getString(request, "appId");
        WXPayNotify notify = null;
        try {
            notify = WebUtils.getJsonRequest(request, WXPayNotify.class);
            log.info("{{0}}微信Native充值回调，回调内容：{{1:json}}", appId, notify);
            WXPayConfig config = WXPayConfigService.self().getByAppId(appId);
            if (notify != null && config != null) {
                WXPayNativePayNotify resultNotify = WXPayUtils.getWXPayNativePayNotify(notify, config.getApiV3Key());
                CloudPayAccountDepositOrderService.self().payResult(resultNotify);
            }
        } catch (Exception e) {
            log.error(appId + "微信Native充值回调错误，" + e.getMessage(), e);
        }
        return new WXPayNotifyResponse();
    }

}
